﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;


namespace EnvironmentManagerSystem.ExecutiveDepartment
{
    public partial class DepartmentDistribute : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return; if (!IsPostBack)
            {
                ddlDepartmentBind();
                if (ddlDepartment.SelectedIndex != -1)
                {
                    BindRoleRight(int.Parse(ddlDepartment.SelectedValue.ToString()));
                    BindRight();
                }
            }

        }

        #region DropDownList_Bind
        protected void ddlDepartmentBind()
        {
            ddlDepartment.DataSource = ExecutiveDepartmentService.GetList();
            ddlDepartment.DataTextField = "ed_Name";
            ddlDepartment.DataValueField = "ed_Id";
            ddlDepartment.DataBind();
        }
        protected void ddlDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindRoleRight(int.Parse(ddlDepartment.SelectedValue.ToString()));
            BindRight();
        }
        #endregion

        #region 绑定所有人员
        protected void BindRight()
        {
            LBRight.DataSource = ExecutiveDepartmentService.GetUserList();
            //LBRight.DataTextField = "UserName" + "(" + "RoleId" + ")";
            LBRight.DataTextField = "UserName";
            LBRight.DataValueField = "ID";
            LBRight.DataBind();
            foreach (ListItem list in LBRoleRight.Items)
            {
                LBRight.Items.Remove(LBRight.Items.FindByValue(list.Value));
            }
        }
        #endregion

        #region 部门下属人员
        protected void BindRoleRight(int Ed_Id)
        {
            string b1 = "";
            string b2 = "";
            string b3 = "";
            string b4 = "";
            DataTable dt = ExecutiveDepartmentService.UserDepartment(Ed_Id);
            if (dt.Rows.Count > 0)
            {
                b1 = dt.Rows[0]["ed_Id"].ToString().ToString();
                b2 = dt.Rows[0]["ed_Name"].ToString().ToString();
                b3 = dt.Rows[0]["ed_User"].ToString().ToString();
                b4 = b3.Trim(',');
                if (b4 != "")
                {
                    LBRoleRight.DataSource = ExecutiveDepartmentService.GetDepartmentUserList(b4);
                    LBRoleRight.DataTextField = "EuserName";
                    LBRoleRight.DataValueField = "EuserId";
                    LBRoleRight.DataBind();
                }
                else
                    LBRoleRight.Items.Clear();
            }

        }
        #endregion

        #region Button_Click
        //增加
        protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.LBRight.SelectedIndex != -1)
            {
                ListItem m_Item = new ListItem(this.LBRight.SelectedItem.Text, this.LBRight.SelectedItem.Value);
                if (!isTrue(LBRoleRight, LBRight.SelectedItem.Value))
                {
                    ExecutiveDepartmentService.InsertUserDepartment(int.Parse(LBRight.SelectedValue), int.Parse(ddlDepartment.SelectedValue));
                    this.LBRoleRight.Items.Add(m_Item);
                }
                this.LBRight.Items.Remove(m_Item);
                if (LBRight.Items.Count > 0)
                    LBRight.SelectedIndex = 0;
            }
        }
        // 删除
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.LBRoleRight.SelectedIndex != -1)
            {
                ListItem m_Item = new ListItem(this.LBRoleRight.SelectedItem.Text, this.LBRoleRight.SelectedItem.Value);
                string d1 = LBRoleRight.SelectedValue;
                //string d2 = "";
                //DataTable dt = ExecutiveDepartmentService.User(d1);
                //if (dt.Rows.Count > 0)
                //{
                //    d2 = dt.Rows[0]["ID"].ToString().ToString();
                //}                
                ExecutiveDepartmentService.DeleteUserDepartment(d1, int.Parse(ddlDepartment.SelectedValue));
                if (!isTrue(LBRight, LBRoleRight.SelectedItem.Value))
                {
                    this.LBRight.Items.Add(m_Item);
                }
                this.LBRoleRight.Items.Remove(m_Item);
                if (LBRoleRight.Items.Count > 0)
                    LBRoleRight.SelectedIndex = 0;
            }
        }
        #endregion


        /// <summary>
        /// 是否有重复
        /// </summary>
        /// <param name="lb">ListBoxID</param>
        /// <param name="Value">对比值</param>
        /// <returns>bool值</returns>
        private bool isTrue(ListBox lb, string Value)
        {
            bool m_temp = false;
            for (int i = 0; i < lb.Items.Count; i++)
            {
                if (lb.Items[i].Value == Value)
                {
                    return m_temp = true;
                }
            }
            return m_temp;
        }
    }
}
